home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / Folder・Icon・Opener 1.0.1.sit / Folder•Icon•Opener 1.0.1 / source code / sources / FIOpen.copy.c < prev    next >
Text File  |  1996-05-05  |  6KB  |  226 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * FIOpen.copy.c
  4.  *--------------------------------------------------------------
  5.  *    Copyright ゥ Fumio Rokkaku, 1996
  6.  *--------------------------------------------------------------
  7.  */
  8. #pragma once
  9.  
  10. /* System Headers */
  11. #include <Memory.h>
  12. #include <QuickDraw.h>
  13. #include <QDOffscreen.h>
  14. #include <OSUtils.h>
  15. #include <Scrap.h>
  16. #include <icons.h>
  17. #include <Files.h>
  18. #include <Finder.h>
  19. #include <Gestalt.h>
  20. #include <Errors.h>
  21.  
  22. /* Project Headers */
  23. #include "FIOpen.h"
  24.  
  25. /*
  26.  *--------------------------------------------------------------
  27.  *    static function prototypes
  28.  *--------------------------------------------------------------
  29.  */
  30. pascal OSErr CopyIconToScrap(ResType, Handle *, void *);
  31. static OSErr CopyIconsPictToScrap(Handle);
  32. static OSErr MakeIconsPict(Handle, PicHandle *);
  33.  
  34. /*
  35.  *--------------------------------------------------------------
  36.  * GetIconToScrap
  37.  *--------------------------------------------------------------
  38.  *    get an icon suite of the folder and copy it to the clipboard
  39.  *--------------------------------------------------------------
  40.  */
  41. OSErr GetIconToScrap(const FSSpecPtr theSpec)
  42. {
  43.     Handle    aSuite = nil;
  44.     long    gestaltAnswer;
  45.     OSErr    result;
  46.  
  47.     /* check scriptable Finder presense */
  48.     result = Gestalt(gestaltFinderAttr, &gestaltAnswer);
  49.     if (result != noErr || (gestaltAnswer & (1 << gestaltOSLCompliantFinder)) == 0) {
  50.         return (noErr);
  51.     }
  52.     /* show copying message */
  53.     OpenCopyingDialog();
  54.  
  55.     /* then get the icon suite */
  56.     result = GetIconSuiteViaFinder(theSpec, &aSuite);
  57.     if ((result == noErr) && (aSuite != nil)) {
  58.         IconActionUPP    CopyIconUPP = nil;
  59.  
  60.         ShowCopyingIcon(aSuite);
  61.  
  62.         CopyIconUPP = NewIconActionProc(CopyIconToScrap);
  63.         if (CopyIconUPP != nil) {
  64.             ZeroScrap();
  65.             result = ForEachIconDo(aSuite, kSelectorAllAvailableData, CopyIconUPP, nil);
  66.             DisposeRoutineDescriptor(CopyIconUPP);
  67.             if (result == noErr) {
  68.                 CopyIconsPictToScrap(aSuite);
  69.             }
  70.         } else {
  71.             result = MemError();
  72.         }
  73.     }
  74.  
  75.     /* cleanup */
  76.     if (aSuite != nil) {
  77.         DisposeIconSuite(aSuite, false);
  78.     }
  79.     CloseCopyingDialog();
  80.  
  81.     return (result);
  82. }
  83. /*
  84.  *--------------------------------------------------------------
  85.  * CopyIconToScrap
  86.  *--------------------------------------------------------------
  87.  *    copy each icon of the icon suite to clipboard
  88.  *--------------------------------------------------------------
  89.  */
  90. pascal OSErr CopyIconToScrap(ResType theType, Handle *theIcon, void *usrPtr)
  91. {
  92. #pragma unused (usrPtr)
  93.  
  94.     Handle    anIcon = *theIcon;
  95.     OSErr    result = noErr;
  96.  
  97.     if (anIcon != nil) {
  98.         Size    itsSize;
  99.  
  100.         itsSize = GetHandleSize(anIcon);
  101.         if (itsSize > 0) {
  102.             UInt8    hState = HGetState(anIcon);
  103.             HLock(anIcon);
  104.             result = PutScrap(itsSize, theType, *anIcon);
  105.             HSetState(anIcon, hState);
  106.         }
  107.     }
  108.     return (result);
  109. }
  110. /*
  111.  *--------------------------------------------------------------
  112.  * CopyIconsPictToScrap
  113.  *--------------------------------------------------------------
  114.  *    make a picture of the suite and copy it to the clipboard
  115.  *--------------------------------------------------------------
  116.  */
  117. static OSErr CopyIconsPictToScrap(Handle theSuite)
  118. {
  119.     OSErr    result = unimpErr;
  120.  
  121.     /* check the offscreen graphic capability */
  122.     if (gQDVersion >= gestalt32BitQD) {
  123.         PicHandle    aPicture = nil;
  124.  
  125.         result = MakeIconsPict(theSuite, &aPicture);
  126.         if ((result == noErr) && (aPicture != nil)) {
  127.             Size    itsSize = GetHandleSize((Handle)aPicture);
  128.             HLock((Handle)aPicture);
  129.             result = PutScrap(itsSize, 'PICT', *aPicture);
  130.             HUnlock((Handle)aPicture);
  131.         }
  132.         if (aPicture != nil) {
  133.             KillPicture(aPicture);
  134.         }
  135.     }
  136.     return (result);
  137. }
  138. /*
  139.  *--------------------------------------------------------------
  140.  * MakeIconsPict
  141.  *--------------------------------------------------------------
  142.  *    create a picture of the icon suite using GWorld
  143.  *--------------------------------------------------------------
  144.  */
  145. static OSErr MakeIconsPict(Handle theSuite, PicHandle *thePict)
  146. {
  147.     PicHandle    aPict = nil;
  148.     OSErr        result = noErr;
  149.  
  150.     if (theSuite != nil) {
  151.         GWorldPtr    aGWorld1 = nil;
  152.         GWorldPtr    aGWorld2 = nil;
  153.         Rect        iconRect;
  154.         RectPtr        aBounds = &iconRect;
  155.  
  156.         SetRect(aBounds, 0, 0, 32, 32);
  157.         result = NewGWorld(&aGWorld1, 8, aBounds, nil, nil, 0);
  158.         if (result == noErr) {
  159.             result = NewGWorld(&aGWorld2, 8, aBounds, nil, nil, 0);
  160.         }
  161.         if (result == noErr) {
  162.             GrafPtr            savePort;
  163.             GWorldPtr        saveGWld;
  164.             GDHandle        saveGDev;
  165.             PixMapHandle    aPixMap1 = nil;
  166.             PixMapHandle    aPixMap2 = nil;
  167.  
  168.             /* get the ofscreen pixcel maps and lock them */
  169.             aPixMap1 = GetGWorldPixMap(aGWorld1);
  170.             aPixMap2 = GetGWorldPixMap(aGWorld2);
  171.             LockPixels(aPixMap1);
  172.             LockPixels(aPixMap2);
  173.  
  174.             /* save current offscreen devices */
  175.             GetPort(&savePort);
  176.             GetGWorld(&saveGWld, &saveGDev);
  177.  
  178.             /* plot the icon suite to the GWorld 1 */
  179.             SetGWorld(aGWorld1, nil);
  180.             ForeColor(blackColor);
  181.             BackColor(whiteColor);
  182.             EraseRect(aBounds);
  183.             result = PlotIconSuite(aBounds, kAlignNone, kTransformNone, theSuite);
  184.  
  185.             /* make a picture of the suite */
  186.             if (result == noErr) {
  187.                 RgnHandle    maskRgn = NewRgn();
  188.                 /* make an icon mask of the suite*/
  189.                 result = IconSuiteToRgn(maskRgn, aBounds, kAlignNone, theSuite);
  190.                 if (result == noErr) {
  191.                     /* copy the GWorld 1 PixMap into the GWorld 2 */
  192.                     SetGWorld(aGWorld2, nil);
  193.                     ForeColor(blackColor);
  194.                     BackColor(whiteColor);
  195.                     EraseRect(aBounds);
  196.                     /* record the GWorld 2 PixMap into the picture */
  197.                     aPict = OpenPicture(aBounds);
  198.                     if (aPict != nil) {
  199.                         SetClip(maskRgn);
  200.                         CopyBits((BitMapPtr)*aPixMap1, (BitMapPtr)*aPixMap2,
  201.                             aBounds, aBounds, srcCopy, maskRgn);
  202.                         ClosePicture();
  203.                     }
  204.                 }
  205.                 DisposeRgn(maskRgn);
  206.             }
  207.             /* unlock the offscreen pixcel maps */
  208.             UnlockPixels(aPixMap1);
  209.             UnlockPixels(aPixMap2);
  210.  
  211.             /* restore saved offscreen devices */
  212.             SetGWorld(saveGWld, saveGDev);
  213.             SetPort(savePort);
  214.         }
  215.         /* cleanups */
  216.         if (aGWorld1 != nil) {
  217.             DisposeGWorld(aGWorld1);
  218.         }
  219.         if (aGWorld2 != nil) {
  220.             DisposeGWorld(aGWorld2);
  221.         }
  222.     }
  223.     *thePict = aPict;
  224.     return (result);
  225. }
  226.